<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Linkslists;
use App\Models\Linkscard;
use App\Models\Memberorders;
use App\Models\Worklink;
use App\Services\OutputService;
use App\Services\HtmlossService;
use Illuminate\Support\Facades\Cache;
use Auth;

class ToolController extends Controller
{
    public function cardAdd(Request $request)
    {
        $users = Auth::user();
        $linkLists = Linkslists::adminUsersLinksSample($users->id);
        $workLinks = Worklink::cardWorkLinkSample($users->id);
        $platfrom = empty($request->platfrom) ? "dy" : $request->platfrom;
        return view("admin.card.card_add",["linkLists"=>$linkLists,"platfrom"=>$platfrom,"workLinks"=>$workLinks]);
    }
    
    public function cardDetial(Request $request)
    {
        if (empty($request->links_id)) return OutputService::error("当前类型暂无链接!");
        $users = Auth::user();
        if ($request->link_type == "wx") {
            $linkList = Linkslists::getLinksListsUsersId($request->links_id);
            if (empty($linkList)) return OutputService::error("微信链接不存在");
            if ($users->authority != 3 && $linkList->users_id != $users->id) return OutputService::error("微信链接不存在");
        } else {
            $workLink = Worklink::getWorkLinkByKfid($request->links_id);
            if (empty($workLink)) return OutputService::error("客服外链链接不存在");
            if ($users->authority != 3 && $workLink->users_id != $users->id) return OutputService::error("客服外链链接不存在");
        }
        $type = empty($request->type) ? "dy" : $request->type;
        $linksCard = Linkscard::getLinksCard($request->links_id,$type);
        if (empty($linksCard)) {
            $linksCard = ["title"=>"","describe"=>"","logo"=>""];
        }
        return OutputService::success("SUCCESS",["linksCard"=>$linksCard]);
    }

    public function cardUpdate(Request $request)
    {
        if (empty($request->links_id)) return OutputService::error("参数错误");
        $users = Auth::user();
        if ($request->link_type == "wx") {
            $linkList = Linkslists::getLinksListsUsersId($request->links_id);
            if (empty($linkList)) return OutputService::error("微信链接不存在");
            if ($users->authority != 3 && $linkList->users_id != $users->id) return OutputService::error("微信链接不存在");
        } else {
            $workLink = Worklink::getWorkLinkByKfid($request->links_id);
            if (empty($workLink)) return OutputService::error("客服外链链接不存在");
            if ($users->authority != 3 && $workLink->users_id != $users->id) return OutputService::error("客服外链链接不存在");
        }
        $memberorder = Memberorders::getLastLinksOrders($users->id);
        if(!empty($memberorder)) {
            if($memberorder->end_time < time()) return OutputService::error("请续费高级版会员后制作/更新抖音卡片");
            if($memberorder->product_id < 3 ) return OutputService::error("请升级高级版会员后制作/更新抖音卡片");
        }
        $type = empty($request->type) ? "dy" : $request->type;
        // 清楚缓存
        Cache::forget('link_card_'.$request->links_id."_".$type);
        $linksCard = Linkscard::getLinksCard($request->links_id,$type);
        if (empty($linksCard)) {
            Linkscard::creatLinksCard($request->links_id,$request->title,$request->describe,$request->logo,$type,$request->link_type);
        } else {
            Linkscard::updateLinksCard($request->links_id,$request->title,$request->describe,$request->logo,$type,$request->link_type);
        }
        if ($type == "dy") {
            // $baseUrl = config("spare_urls.dycardurl")."/pages/".$request->links_id;
            //调用上传到oss
            HtmlossService::uploadHtmlWfx($request->links_id,$request->link_type);
            $baseUrl = config("spare_urls.dycardurl")."/pages/".$request->links_id.".html";
        } else {
            $baseUrl = config("spare_urls.cardurl")."/card/".$request->links_id;
        }
        return OutputService::success("卡片链接复制成功,根据教程完成制作即可!",["card_url"=>$baseUrl]); 
    }

    public function shortAdd(Request $request)
    {
        $users = Auth::user();
        $linkLists = Linkslists::adminUsersLinksSample($users->id);
        return view("admin.shorts.short_add",["linkLists"=>$linkLists]);
    }
}
